<template>
    <div class="app-container">
        <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px"
                 size="small">
            <el-form-item label="机构名称" prop="orgName">
                <el-input
                    v-model="queryParams.orgName"
                    clearable
                    placeholder="请输入机构名称"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    v-hasPermi="['system:SysOrg:add']"
                    icon="el-icon-plus"
                    plain
                    size="mini"
                    type="primary"
                    @click="handleAdd"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    v-hasPermi="['system:SysOrg:export']"
                    icon="el-icon-download"
                    plain
                    size="mini"
                    type="warning"
                    @click="handleExport"
                >导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-adaptive
                  v-loading="loading"
                  :data="SysOrgList"
                  :stripe='true'
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border
                  height="200px"
                  row-key="orgId">
            <el-table-column align="center" label="机构名称" prop="orgName"/>
            <el-table-column align="center" label="机构code" prop="orgCode"/>
            <el-table-column align="center" label="联系人" prop="linkMan"/>
            <el-table-column align="center" label="联系人电话" prop="phone"/>
            <el-table-column align="center" label="排序" prop="orderNum"/>
            <el-table-column align="center" label="备注" prop="remark"/>
            <el-table-column align="center" label="状态" width="100">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.status"
                        active-value="0"
                        inactive-value="1"
                        @change="handleStatusChange(scope.row)"
                    ></el-switch>
                </template>
            </el-table-column>
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
                <template slot-scope="scope">
                    <el-button
                        v-hasPermi="['system:menu:add']"
                        icon="el-icon-plus"
                        size="mini"
                        type="text"
                        @click="handleAdd(scope.row)"
                    >新增
                    </el-button>
                    <el-button
                        v-hasPermi="['system:SysOrg:edit']"
                        icon="el-icon-edit"
                        size="mini"
                        type="text"
                        @click="handleUpdate(scope.row)"
                    >修改
                    </el-button>
                    <el-button
                        v-hasPermi="['system:SysOrg:remove']"
                        icon="el-icon-delete"
                        size="mini"
                        type="text"
                        @click="handleDelete(scope.row)"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :limit.sync="queryParams.pageSize"
            :page.sync="queryParams.pageNum"
            :total="total"
            @pagination="getList"
        />

        <!-- 添加或修改机构对话框 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="50%">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="上级机构" prop="parentId">
                            <treeSelect
                                v-model="form.parentId"
                                :normalizer="normalizer"
                                :options="orgOptions"
                                :show-count="true"
                                placeholder="选择上级机构"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="机构名称" prop="orgName">
                            <el-input v-model="form.orgName" placeholder="请输入机构名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="机构编码" prop="orgCode">
                            <el-input v-model="form.orgCode" :disabled="isEdit" maxlength="16" placeholder="请输入机构code"
                                      show-word-limit/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="机构类型" prop="orgClass">
                            <el-select v-model="form.orgClass" filterable placeholder="请选择机构类型">
                                <el-option
                                    v-for="dict in dict.type.org_class"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="机构等级">
                            <el-select v-model="form.orgLevel" placeholder="请选择机构类型">
                                <el-option
                                    v-for="dict in dict.type.org_level"
                                    :key="dict.value"
                                    :label="dict.label"
                                    :value="dict.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人" prop="linkMan">
                            <el-input v-model="form.linkMan" placeholder="请输入联系人"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人电话" prop="phone">
                            <el-input v-model="form.phone" maxlength="11" placeholder="请输入联系人电话" show-word-limit/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人邮箱" prop="email">
                            <el-input v-model="form.email" placeholder="请输入联系人邮箱"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系人地址" prop="address">
                            <el-input v-model="form.address" placeholder="请输入联系人地址"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="地区名称" prop="region">
                            <el-input v-model="form.region" placeholder="请输入地区名称"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="地区编码" prop="regionCode">
                            <el-input v-model="form.regionCode" placeholder="请输入地区编码"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="社会信用代码" prop="socialCode">
                            <el-input v-model="form.socialCode" placeholder="请输入社会信用代码"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="省" prop="province">
                            <el-input v-model="form.province" placeholder="请输入行政区划代码-省"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="市/区" prop="city">
                            <el-input v-model="form.city" placeholder="请输入行政区划代码-市/区"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="县/区" prop="county">
                            <el-input v-model="form.county" placeholder="请输入行政区划代码-县/区"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" class="select-margin">
                        <el-form-item label="助产标识">
                            <el-radio-group v-model="form.deliverMark">
                                <el-radio
                                    v-for="dict in dict.type.deliver_mark"
                                    :key="dict.value"
                                    :label="dict.value"
                                >{{ dict.label }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" class="select-margin">
                        <el-form-item label="监管标识">
                            <el-radio-group v-model="form.superviseMark">
                                <el-radio
                                    v-for="dict in dict.type.supervise_mark"
                                    :key="dict.value"
                                    :label="dict.value"
                                >{{ dict.label }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" class="select-margin">
                        <el-form-item label="救治中心标识">
                            <el-radio-group v-model="form.cureMark">
                                <el-radio
                                    v-for="dict in dict.type.cure_mark"
                                    :key="dict.value"
                                    :label="dict.value"
                                >{{ dict.label }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" class="select-margin">
                        <el-form-item label="档案关联标识">
                            <el-radio-group v-model="form.archivesMark">
                                <el-radio
                                    v-for="dict in dict.type.archives_mark"
                                    :key="dict.value"
                                    :label="dict.value"
                                >{{ dict.label }}
                                </el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" class="select-margin">
                        <el-form-item label="排序" prop="orderNum">
                            <el-input v-model="form.orderNum" placeholder="请输入排序" type="number"/>
                        </el-form-item>
                    </el-col>

                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构logo">
                            <image-upload v-model="form.orgImg" :limit="1"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="机构介绍" prop="remark">
                            <el-input v-model="form.remark"
                                      :rows="8"
                                      placeholder="请输入内容"
                                      type="textarea"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addSysOrg,
    changeOrgStatus,
    delSysOrg,
    getSysOrg,
    listSysOrg,
    selectPageSysOrg,
    updateSysOrg
} from "@/api/system/sysOrg";
import TreeSelect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
    name: "SysOrg",
    dicts: ['archives_mark', 'org_class', 'deliver_mark', 'org_level', 'supervise_mark', 'cure_mark'],
    components: {TreeSelect},
    data () {
        return {
            // 按钮loading
            buttonLoading: false,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 机构表格数据
            SysOrgList: [],
            // 下拉框机构树
            orgOptions: [],
            // 弹出层标题
            title: "",
            // 是否编辑
            isEdit: false,
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orgName: undefined,
                orgCode: undefined,
                email: undefined,
                address: undefined,
                phone: undefined,
                linkMan: undefined,
                applyStatus: undefined,
                parentId: undefined,
                parentIds: undefined,
                region: undefined,
                regionCode: undefined,
                orgImg: undefined,
                socialCode: undefined,
                province: undefined,
                city: undefined,
                county: undefined,
                deliverMark: undefined,
                superviseMark: undefined,
                cureMark: undefined,
                archivesMark: undefined,
                orgClass: undefined,
                orgLevel: undefined,
                orderNum: undefined,
                status: undefined,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                orgId: [
                    {required: true, message: "机构主键不能为空", trigger: "blur"}
                ],
                parentId: [
                    {required: true, message: "上级机构不能为空", trigger: "blur"}
                ],
                orgName: [
                    {required: true, message: "机构名称不能为空", trigger: "blur"}
                ],
                orgCode: [
                    {required: true, message: "机构code不能为空", trigger: "blur"}
                ]
            },
        };
    },
    watch: {
        'form.orgCode' (newValue, oldValue) {
            this.form.orgCode = newValue.replace(/[\u4e00-\u9fa5/\s+/]|[^a-zA-Z0-9\u4E00-\u9FA5]/g, "")
        }
    },
    created () {
        this.getList();
    },
    methods: {
        /** 转换菜单数据结构 */
        normalizer (node) {
            if (node.children && !node.children.length) {
                delete node.children
            }
            return {
                id: node.orgId,
                label: node.orgName,
                children: node.children
            }
        },
        /** 查询机构列表 */
        getList () {
            this.loading = true;
            listSysOrg(this.queryParams).then(response => {
                this.SysOrgList = this.handleTree(response.data, "orgId")
                this.loading = false
                this.loading = false;
            });
        },
        /** 查询机构下拉树结构 */
        getTreeSelect () {
            listSysOrg().then(response => {
                this.orgOptions = []
                const org = {orgId: 0, orgName: '主类目', children: []}
                org.children = this.handleTree(response.data, "orgId")
                this.orgOptions.push(org)
            })
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 角色状态修改
        handleStatusChange (row) {
            let text = row.status === "0" ? "启用" : "停用";
            this.$modal.confirm('确认要"' + text + '""' + row.orgName + '"机构吗？').then(function () {
                return changeOrgStatus(row.orgId, row.status);
            }).then(() => {
                this.$modal.msgSuccess(text + "成功");
            }).catch(function () {
                row.status = row.status === "0" ? "1" : "0";
            });
        },
        // 表单重置
        reset () {
            this.form = {
                orgId: undefined,
                orgName: undefined,
                orgCode: undefined,
                email: undefined,
                address: undefined,
                phone: undefined,
                linkMan: undefined,
                applyStatus: "0",
                parentId: undefined,
                parentIds: undefined,
                region: undefined,
                regionCode: undefined,
                orgImg: undefined,
                socialCode: undefined,
                province: undefined,
                city: undefined,
                county: undefined,
                deliverMark: "0",
                superviseMark: "0",
                cureMark: "0",
                archivesMark: "0",
                orgClass: "0",
                orgLevel: "1",
                orderNum: undefined,
                delFlag: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                remark: undefined,
                status: '0'
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd (row) {
            this.reset();
            this.open = true;
            this.isEdit = false
            this.getTreeSelect()
            if (row != null && row.orgId) {
                this.form.parentId = row.orgId
            } else {
                this.form.parentId = 0
            }
            this.title = "添加机构";
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.loading = true;
            this.isEdit = true
            this.reset();
            this.getTreeSelect()
            const orgId = row.orgId || this.ids
            getSysOrg(orgId).then(response => {
                this.loading = false;
                this.form = response.data;
                this.open = true;
                this.title = "修改机构";
            });
        },
        /** 提交按钮 */
        submitForm () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    this.buttonLoading = true;
                    if (this.form.orgId != null) {
                        updateSysOrg(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    } else {
                        addSysOrg(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        }).finally(() => {
                            this.buttonLoading = false;
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const orgIds = row.orgId || this.ids;
            this.$modal.confirm('是否确认删除机构编号为"' + orgIds + '"的数据项？').then(() => {
                this.loading = true;
                return delSysOrg(orgIds);
            }).then(() => {
                this.loading = false;
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            }).finally(() => {
                this.loading = false;
            });
        },
        /** 导出按钮操作 */
        handleExport () {
            this.download('system/SysOrg/export', {
                ...this.queryParams
            }, `SysOrg_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>

<style scoped>
.freePic ::v-deep .hide .el-upload--picture-card {
    display: none;
}
</style>
